<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="收货人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['consigneeName']" placeholder="请输入收货人"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="省市区" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--              <j-area-linkage type="cascader" v-decorator="['pcu']" placeholder="请输入省市区"/>-->
              <j-area-linkage type="select" v-decorator="['county']" placeholder="请输入省市区" @change="change" />
            </a-form-item>
          </a-col>
          <!--<a-col :span="24">
            <a-form-item label="省" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['province']" placeholder=""></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="市" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['city']" placeholder=""></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="区" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['county']" placeholder=""></a-input>
            </a-form-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-item label="详细收货地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['addressDetail']" placeholder="请输入详细收货地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="邮政编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['postalCode']" placeholder="请输入邮政编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['tel']" placeholder="请输入手机号码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否默认地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isDefault']" :trigger-change="true" dictCode="is_default" placeholder="请选择是否默认地址"/>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JAreaLinkage from '@comp/jeecg/JAreaLinkage'
  import Area from '@/components/_util/Area'
  export default {
    name: 'CustomerAddressForm',
    components: {
      JFormContainer,
      JDictSelectTag,
      JAreaLinkage,
    },
    props: {
      //传递进来的客户ID
      // customerid:['customerid'],
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        //传递进来的客户ID
        customerid:'',

        pcu:'',
        //省，市，区
        sheng:'',
        shi:'',
        qu:'',
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/member/customerAddress/add",
          edit: "/member/customerAddress/edit",
          queryById: "/member/customerAddress/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
      this.pcaData = new Area()
    },
    methods: {
      add (e) {
        // console.log('e',e)
        this.customerid = e
        /*
        * 新增地址按钮 获取customerId，写入到表单record里进行提交
        * */
        console.log('customerid-add',this.customerid)
        this.edit({customerId:e});
      },
      edit (record) {
        console.log('edit-btn',record.customerId)
        // if(!record.customerId){
          this.customerid = record.customerId
        // }
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'consigneeName','pcu','province','city','county','addressDetail','postalCode','tel','isDefault'))
          this.pcu = record.pcu
          console.log('pcu',this.pcu)
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values,{pcu:this.pcu,customerId:this.customerid,province:this.sheng,city:this.shi,county:this.qu});
            console.log("表单提交数据",formData)
            //暂时关闭上传，为了查看提交的数据
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'consigneeName','pcu','province','city','county','addressDetail','postalCode','tel','isDefault'))
      },
      change(res1,res2){
        // console.log('省市区数组1',res1)
        console.log('省市区数组2',res2)
        // console.log('省市区数组2',this.model)
        this.sheng = res2[0]
        this.shi = res2[1]
        this.qu = res2[2]
        let pcu = this.pcaData.getText(this.qu);
        console.log('text',pcu)
        this.pcu = pcu
      }
    }
  }
</script>